import axios from "axios";
export function allDownload(data, titName) {
  if (!data) {
    return;
  }
  var blob = new Blob([data]);
  if ("download" in document.createElement("a")) {
    const eleLink = document.createElement("a");
    eleLink.download = titName;
    eleLink.style.display = "none";
    eleLink.href = data;
    // 触发点击
    document.body.appendChild(eleLink);
    eleLink.click();
    // 然后移除
    document.body.removeChild(eleLink);
  } else {
    // IE10+下载
    navigator.msSaveBlob(blob, titName);
  }
}
export function fileDownload(data, titName) {
  const x = new window.XMLHttpRequest();
  x.open('GET', data, true);
  x.responseType = 'blob';
  x.onload = () => {
    const url = window.URL.createObjectURL(x.response);
    const a = document.createElement('a');
    a.href = url;
    a.target = '_blank'
    a.download = titName;
    a.style.display = 'none'
    document.body.append(a)
    a.click();
  };
  x.send();
}
export function newfileDownload(data, titName) {
  const x = new XMLHttpRequest()
  x.open('GET', data, true)
  x.responseType = 'blob'
  x.onload = e => {
    // 会创建一个 DOMString，其中包含一个表示参数中给出的对象的URL。这个 URL 的生命周期和创建它的窗口中的 document 绑定。这个新的URL 对象表示指定的 File 对象或 Blob 对象。
    const url = window.URL.createObjectURL(x.response)
    const a = document.createElement('a')
    a.href = url
    a.download = titName
    a.click()
  }
  x.send()
}
// export function fileDownload(data, titName, fileType) {
//   if (!data) {
//     return;
//   }
//   let fileTypeContent = "";
//   if (fileType.indexOf("pdf") != -1) {
//     fileTypeContent = "application/pdf";
//   } else if (fileType.indexOf("xls") != -1 || fileType.indexOf("xlsx") != -1) {
//     fileTypeContent = "application/vnd.ms-excel";
//   } else if (fileType.indexOf("doc") != -1) {
//     fileTypeContent = "application/msword";
//   } else if (fileType.indexOf("docx") != -1) {
//     fileTypeContent = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
//   } else if (fileType.indexOf("ppt") != -1) {
//     fileTypeContent = "application/vnd.ms-powerpoint";
//   } else if (fileType.indexOf("json") != -1) {
//     fileTypeContent = "application/json";
//   } else if (fileType.indexOf("txt") != -1) {
//     fileTypeContent = "text/plain";
//   } else if (fileType.indexOf("jpeg") != -1 || fileType.indexOf("jpg") != -1) {
//     fileTypeContent = "image/jpeg";
//   } else if (fileType.indexOf("png") != -1) {
//     fileTypeContent = "image/png";
//   } else if (fileType.indexOf("gif") != -1) {
//     fileTypeContent = "image/gif";
//   } else if (fileType.indexOf("zip") != -1) {
//     fileTypeContent = "application/zip";
//   } else if (fileType.indexOf("rar") != -1) {
//     fileTypeContent = "application/x-rar-compressed";
//   }
//   console.log(fileTypeContent)
//   var blob = new Blob([data], { type: fileTypeContent });
// }
export function download(data, titName) {
  if (!data) {
    return;
  }
  const content = data;
  const blob = new Blob([content], {
    type: "application/vnd.ms-excel;charset=utf-8",
  });
  const fileName = titName; //这个为文件名，可以自定义
  if ("download" in document.createElement("a")) {
    // 非IE下载
    let downloadElement = document.createElement("a");
    downloadElement.href = data;
    downloadElement.download = fileName;
    document.body.appendChild(downloadElement);
    downloadElement.click();
    document.body.removeChild(downloadElement);
    window.URL.revokeObjectURL(data);
  } else {
    // IE10+下载
    navigator.msSaveBlob(blob, fileName);
  }
}
export function downloadImg(imgsrc, name) {
  var image = new Image()
  image.setAttribute('crossOrigin', 'anonymous')
  image.onload = function () {
    var canvas = document.createElement('canvas')
    canvas.width = image.width
    canvas.height = image.height
    var context = canvas.getContext('2d')
    context.drawImage(image, 0, 0, image.width, image.height)
    var url = canvas.toDataURL('image/png')
    var a = document.createElement('a')
    var event = new MouseEvent('click')
    a.download = name || 'photo'
    a.href = url
    a.dispatchEvent(event)
  }
  image.src = imgsrc
  // if (!data) {
  //   return;
  // }
  // var img = document.getElementById("testImg"); // 获取要下载的图片
  // var url = data; // 获取图片地址
  // var a = document.createElement("a"); // 创建一个a节点插入的document
  // const fileName = `${titName}.jpg`;
  // var event = new MouseEvent("click"); // 模拟鼠标click点击事件
  // a.download = fileName; // 设置a节点的download属性值
  // a.href = url; // 将图片的src赋值给a节点的href
  // a.dispatchEvent(event); // 触发鼠标点击事件

  // const fileName = `${titName}.jpg`;
  // const myBlob = new Blob([data], { type: "image/jpeg" });
  // const link = document.createElement("a");
  // link.href = URL.createObjectURL(myBlob);
  // link.download = fileName;
  // link.click();
  // link.remove();
  // URL.revokeObjectURL(link.href);
  // getUrlBase64(data).then((base64) => {
  //   let a = document.createElement("a");
  //   a.style.display = "none";
  //   a.download = titName;
  //   a.href = base64;
  //   document.body.appendChild(a);
  //   a.click();
  // });
}
export function downloadpdf(data, titName) {
  if (!data) {
    return;
  }
  const content = data;
  const blob = new Blob([content], {
    type: "application/pdf;charset=utf-8",
  }); //重点重点，，，
  //application/msword类型要规定对，自己下载的是什么类型就写对应的类型
  const fileName = titName; //这个为文件名，可以自定义
  if ("download" in document.createElement("a")) {
    // 非IE下载
    let downloadElement = document.createElement("a");
    downloadElement.href = data;
    downloadElement.download = fileName;
    document.body.appendChild(downloadElement);
    downloadElement.click();
    document.body.removeChild(downloadElement);
    window.URL.revokeObjectURL(data);
  } else {
    // IE10+下载
    navigator.msSaveBlob(blob, fileName);
  }
}
export function downloadpdf1(data, titName) {
  if (!data) {
    return;
  }
  const content = data;
  const blob = new Blob([content], {
    type: "application/vnd.ms-excel;charset=utf-8",
  });
  const fileName = titName ? titName : "报废卡号.xlsx";
  if ("download" in document.createElement("a")) {
    // 非IE下载
    const elink = document.createElement("a");
    elink.download = fileName;
    elink.style.display = "none";
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink);
  } else {
    // IE10+下载
    navigator.msSaveBlob(blob, fileName);
  }
  // const fileName = titName; //这个为文件名，可以自定义
  // if ("download" in document.createElement("a")) {
  //   // 非IE下载
  //   let downloadElement = document.createElement("a");
  //   downloadElement.href = data;
  //   downloadElement.download = fileName;
  //   document.body.appendChild(downloadElement);
  //   downloadElement.click();
  //   document.body.removeChild(downloadElement);
  //   window.URL.revokeObjectURL(data);
  // } else {
  //   // IE10+下载
  //   navigator.msSaveBlob(blob, fileName);
  // }
}
export function downloadworld(data, titName) {
  if (!data) {
    return;
  }
  const content = data;
  const blob = new Blob([content], {
    type: "application/msword;charset=utf-8",
  }); //重点重点，，，
  //application/msword类型要规定对，自己下载的是什么类型就写对应的类型
  const fileName = titName; //这个为文件名，可以自定义
  if ("download" in document.createElement("a")) {
    // 非IE下载
    let downloadElement = document.createElement("a");
    downloadElement.href = data;
    downloadElement.download = fileName;
    document.body.appendChild(downloadElement);
    downloadElement.click();
    document.body.removeChild(downloadElement);
    window.URL.revokeObjectURL(data);
  } else {
    // IE10+下载
    navigator.msSaveBlob(blob, fileName);
  }
}
export function getUrlBase64(url) {
  return new Promise((resolve) => {
    let canvas = document.createElement("canvas");
    let ctx = canvas.getContext("2d");
    let img = new Image();
    img.crossOrigin = "Anonymous"; //允许跨域
    img.src = url;
    img.onload = function () {
      canvas.height = 300;
      canvas.width = 300;
      ctx.drawImage(img, 0, 0, 300, 300);
      let dataURL = canvas.toDataURL("image/png");
      canvas = null;
      resolve(dataURL);
    };
  });
}
